// profile.js
var app = getApp();
var iFDU = require('../../utils/iFDU.js');
Page({
  data: {
    config: app.config,
    globalData: app.globalData,
    balance: {},
    orderStatus: {
      1: '待支付',
      2: '全速定制中...',
      3: '已发货', 
      5: '已完成',
      13: '用户取消',
      15: '系统取消'
    },
    orders: [],
  },

  showTips: function () {
    wx.showModal({
      content: '账户余额是您在“逸定制”“逸见服饰”商城中的通用预充值款项当前可用金额，在购物时可1:1充当现金使用，可以购买“逸定制”“逸见服饰”商城中的常规商品。（余额不可用于购买充值卡或特殊商品：由于充值卡发售渠道不同，余额与红包有可能出现不可同时使用的情况。）如需购买充值礼品卡，请点击“购买特惠充值卡”选购。感谢您的支持！更多详情可咨询微信客服。',
      showCancel: false,
      confirmText: "知道了",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        }
      }
    });
  },

  showRealRedbag: function () {
    wx.navigateTo({
      url: '../real-redbag/real-redbag',
    });
  },

  showShareRedbag: function () {
    wx.navigateTo({
      url: '../share-redbag/share-redbag',
    });
  },

  onLoad: function (options) {
    var that = this;
  },

  onShow: function () {
    var that = this;

    app.getUserInfo(function (globalData) {
      that.setData({
        globalData: globalData
      });

      that.loadOrders();

      iFDU.api.doGet(
        'user/getAmountInMyPage',
        {
          userId: that.data.globalData.userId,
        },
        function (data) {
          that.setData({
            balance: data
          });
        }
      );
    });
  },

  loadOrders: function () {
    var that = this;
    iFDU.api.doGet(
      'order/getOrderList',
      {
        userId: that.data.globalData.userId,
        type: 0,
        limit: 1,
        rnd: Math.random()
      },
      function (data) {
        var orders = data;
        if (orders && orders.length > 0) {
          for (var i = 0; i < orders.length; i++) {
            var orderItems = orders[i].orderDetailViewList;
            var needCustomize = false;
            if (orderItems && orderItems.length > 0) {
              for (var j = 0; j < orderItems.length; j++) {
                var orderItem = orderItems[j];
                var product = orderItem.mainProductView;
                if (product && product.is_virtual == 0 && product.is_broider == 1) {
                  needCustomize = true;
                  break;
                }
              }
            }
            orders[i].needCustomize = needCustomize;
          }
        }
        that.setData({
          orders: orders
        });
      }
    );
  },

  showOrderDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../order-detail/order-detail?orderId=' + id,
    })
  },

  deleteOrder: function (e) {
    var that = this;
    wx.showModal({
      title: '删除订单',
      content: '将此订单从列表中永久删除吗？',
      confirmText: '删除',
      cancelText: '保留',
      success: function (res) {
        if (res.confirm) {
          iFDU.api.doGet(
            'order/deleteOrder',
            {
              userId: that.data.globalData.userId,
              orderId: e.currentTarget.dataset.id,
            },
            function (data) {
              that.loadOrders();
            }
          );
        }
      }
    })
  },

  cancelOrder: function (e) {
    var that = this;
    wx.showModal({
      title: '取消订单',
      content: '确定取消此订单吗？',
      confirmText: '取消订单',
      cancelText: '先不取消',
      success: function (res) {
        if (res.confirm) {
          iFDU.api.doGet(
            'order/cancelOrder',
            {
              userId: that.data.globalData.userId,
              orderId: e.currentTarget.dataset.id,
            },
            function (data) {
              that.loadOrders();
            }
          );
        }
      }
    })
  },

  showCSTips: function () {
    wx.showModal({
      content: '如果您需要联系我们或售后问题，请在服务号中发起会话，或添加逸见服饰小秘书微信进行及时沟通！微信号：yjshirt',
      showCancel: false,
      confirmText: "知道了",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        }
      }
    });
  },

  showBodyDataList: function () {
    //wx.navigateTo({
      //url: '../body-data-list/body-data-list',
    //})
  },

  showAddressList: function () {
    wx.navigateTo({
      url: '../address-list/address-list',
    })
  },

  viewExpress: function (e) {
    var deliverCompany = e.currentTarget.dataset.deliver_company;
    var deliverOrderId = e.currentTarget.dataset.deliver_order_id;
    wx.navigateTo({
      url: '../trace-detail/trace-detail?deliverCompany=' + deliverCompany + '&deliverOrderId=' + deliverOrderId,
    })
  },

  payOrder: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var data = {
      userId: that.data.globalData.userId,
      orderId: orderId,
    };
    iFDU.api.doGet('payment/getInfoForPayment', data, function (data) {
      wx.navigateTo({
        url: '../goods-pay/goods-pay?orderId=' + orderId,
      })
    });
  },

  showOrderList: function (e) {
    wx.navigateTo({
      url: '../order-list/order-list',
    })
  },

  goRecharge: function (e) {
    wx.navigateTo({
      url: '../recharge/recharge',
    })
  },

  goHome: function () {
    wx.switchTab({
      url: '../home/home',
    })
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '爱上衬衫，从「逸见」开始…',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/yjshare.jpeg',
      path: 'pages/home/home',
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  }
})